package com.study.demo.es;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TaskEsTopicConfig {

    private static final int HTTP_PORT = 9200;
    private static final String IP = "10.0.0.28";
    private static final String SecurityUser = "elastic";
    private static final String SecurityPassword = "123456";


//    private static CredentialsProvider init() {
//        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(SecurityUser, SecurityPassword));
//        return credentialsProvider;
//    }

//    @Bean
//    public RestClientBuilder restClientBuilder() {
//        //配置权限验证
//        RestClientBuilder restClientBuilder = RestClient.builder( new HttpHost(IP, HTTP_PORT, "http"))
//                .setHttpClientConfigCallback(httpClientBuilder ->
//                        httpClientBuilder.setDefaultCredentialsProvider(init()));
//        return restClientBuilder;
//    }


//    @Bean(name = "highLevelClient")
//    public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
//        return new RestHighLevelClient(restClientBuilder);
//    }

//    @Bean(name = "client")
//    public RestHighLevelClient getRestHighLevelClient() {
//        // 阿里云ES集群需要basic auth验证。
//        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//
//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
//        RestClientBuilder builder = RestClient.builder(new HttpHost("10.0.0.28", 9200)).setHttpClientConfigCallback(
//                new RestClientBuilder.HttpClientConfigCallback() {
//                    @Override
//                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                    }
//                });
//        return new RestHighLevelClient(builder);
//    }

}
